<?php
 
 require_once './../config.php'; 
 require_once "./language/".ADMIN_LANGUAGE.".inc.php";
 
?>

function isNumberValue (val)
{
    var num = parseFloat(val) + '';
    if (val!=''+num && val!=''+num+'0') 
    {
        alert('<?php echo ALL_MSG_MustBeNumeric; ?>.');
        return false;
    }    
    else
        return true;
}
function isNumberIntegerValue (val)
{
    var num = parseInt(val) + '';
    if (val!=''+num) 
    {
        alert('<?php echo ALL_MSG_MustBeNumeric; ?>.');
        return false;
    }    
    else
        return true;
}
function isNumberIntegerOnForm (val)
{
    var num = parseInt(val) + '';
    return ! (val!=''+num);
}


var err = '';
var firstFieldError = '';

function init_validate()
{
    err = '';
    firstFieldError = '';
}

function locate(item)
{
   for(i=0;i < document.FormEdit.elements.length;i++) 
    {
      
        if (document.FormEdit.elements[i].name == item)
        {    
          return i;
        }
    }
    return -1;    
}

function allok()
{
    if (err != '')
    {
        alert ('<?php echo ALL_MSG_ErrorsHappen; ?>:\n'+err);
        navegador = navigator.appName;
        if (navegador.indexOf("Explorer") != -1) 
           document.FormEdit(firstFieldError).focus();
        return false;
    }
    else
        return true;
}

function isRequired (field, name)   
{
    if (document.FormEdit.elements[ locate(field) ].value=="")
    {      
        err = err  + ' - '+name+' <?php echo ALL_MSG_Required; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;
    }
}

function isRequiredEmpty (field, name)
{
    if (document.FormEdit.elements[ locate(field) ].value != "")
    {
        err = err  + ' - '+name+'.\n';
        if (firstFieldError == '') firstFieldError = field;
    }
}

function isRequiredRadio (field, name)
{
    var saved = 0;
    for(i=0;i < document.FormEdit.elements.length;i++) 
    {
      
        if (document.FormEdit.elements[i].type == "radio" && document.FormEdit.elements[i].name == field)
        {    
            saved = i;
            if (document.FormEdit.elements[i].checked)
                return;
        }    
    }
    err = err  + ' - '+name+' <?php echo ALL_MSG_Required; ?>.\n';
    if (firstFieldError == '') firstFieldError = saved;
}

function isEmail (field, name)
{
    var val = document.FormEdit.elements[ locate(field) ].value;
    var p = val.indexOf('@');
    if ((p < 1 || p==(val.length-1)) && document.FormEdit.elements[ locate(field) ].value != '')
    {
        err = err  + ' - '+name+' <?php echo ALL_MSG_MustBeEmail; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;
    }    
}

function isEmailR (field, name)
{ 
    var val = document.FormEdit.elements[ locate(field) ].value;
    var p = val.indexOf('@');
    if (p < 1 || p == (val.length-1))
    {
        err = err  + ' - '+name+' <?php echo ALL_MSG_MustBeEmail; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;        
    }    
}

function isNumber (field, name)
{
    var val = document.FormEdit.elements[ locate(field) ].value;
    var num = parseFloat(val) + '';
    if (val!=''+num && val!=''+num+'0' && val!=''+num+'.00') 
    {
        err+= ' - '+name+' <?php echo ALL_MSG_MustBeNumber; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;        
    }    
}

function isNumberInt (field, name)
{
    var val = document.FormEdit.elements[ locate(field) ].value;
    var num = parseInt(val);
    if (val!=''+num) 
    {
        err+= ' - '+name+' <?php echo ALL_MSG_MustBeNumber; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;        
    }    
}

function isNumberPositive (field, name)
{
    var val = document.FormEdit.elements[ locate(field) ].value;
    var num = parseFloat(val)+'';
    if ((val!=''+num && val!=''+num+'0') || num < 0) 
    {
        err+= ' - '+name+' <?php echo ALL_MSG_MustBeNumberGreater; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;        
    }   
}

function isDate(fd, fm, fy, name, start_year) 
{
    var day   = document.FormEdit.elements[ locate(fd) ].selectedIndex + 1;
    var month = document.FormEdit.elements[ locate(fm) ].selectedIndex + 1;
    var year  = document.FormEdit.elements[ locate(fy) ].selectedIndex + parseInt(document.FormEdit.elements[ locate(fy) ][0].value);
                             
    var result = true;
    var errString = "";
                          
    if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) 
    {
        err = err  + ' - '+name+' <?php echo ALL_MSG_ValidDate1; ?>. \n';
        if (firstFieldError == '') firstFieldError = fd;
    } 
    else 
    { 
        if (day >= 30 && month == 2) 
        {
            err = err  + ' - '+name+' <?php echo ALL_MSG_ValidDate2; ?>.\n';
            if (firstFieldError == '') firstFieldError = fd;            
        } 
        else if (day == 29 && month == 2 && ((year % 4) != 0)) 
        {
            err = err  + ' - '+name+' <?php echo ALL_MSG_ValidDate3; ?>.\n';
            if (firstFieldError == '') firstFieldError = fd;            
        } 
    }   
}

function isOrderedSemiDate (m1,y1,m2,y2,name)
{
    var month1 = document.FormEdit.elements[ locate(m1) ].selectedIndex + 1;
    var year1  = document.FormEdit.elements[ locate(y1) ].selectedIndex + parseInt(document.FormEdit.elements[ locate(y1) ][0].value);
    var month2 = document.FormEdit.elements[ locate(m2) ].selectedIndex + 1;
    var year2  = document.FormEdit.elements[ locate(y2) ].selectedIndex + parseInt(document.FormEdit.elements[ locate(y2) ][0].value);
   
    if (year2 < year1 || (year2==year1 && month2 < month1))
    {
        err += ' - '+name+'.\n';
        if (firstFieldError == '') firstFieldError = m1; 
    }
}

function getRadioValue (field)
{
    for(i=0;i < document.FormEdit.elements.length;i++)       
        if (document.FormEdit.elements[i].type == "radio" && document.FormEdit.elements[i].name == field && document.FormEdit.elements[i].checked)     
            return document.FormEdit.elements[i].value;
    return '';
}

function compareDate (d1, d2)
{
  d1obj = new Date (Date.parse(d1));
  d2obj = new Date (Date.parse(d2));  
  d1value = d1obj.getYear() * 10000 + d1obj.getMonth() * 100 + d1obj.getDate();
  d2value = d2obj.getYear() * 10000 + d2obj.getMonth() * 100 + d2obj.getDate();
  if (d1value > d2value)
    return -1;
  else
  {
      if (d1value < d2value)
          return 1;
      else
          return 0;
  }  
}

function isFileRequired(field, fieldhidden, name)
{
	if ((document.FormEdit.elements[ locate(field) ].value=="") && (document.FormEdit.elements[ locate(fieldhidden) ].value==""))
	{
		err = err  + ' - '+name+' <?php echo ALL_MSG_Required; ?>.\n';
        if (firstFieldError == '') firstFieldError = field;
	}
}

function isExtValid (field, name,ext)   
{   
	value = document.FormEdit.elements[ locate(field) ].value	
    if ((value!="") && (ext!=""))
    {      
         var tmp = "";
		 for(i=0; i <= ext.length; i++)
			 if (ext.charAt(i) == ";" || i == ext.length)
			 {
				 var tmp2 = "";               
				 for(j = value.length - 1; j > value.length - tmp.length - 2; j--)
					 tmp2 = value.charAt(j) + tmp2;
				 if (tmp2 == "."+tmp)
				     return;
				 tmp = "";
			 }
			 else
				 tmp += ext.charAt(i);
		 err+= ' - '+name+' <?php echo ALL_MSG_ValidExt; ?>.\n';
         if (firstFieldError == '') firstFieldError = field; 		 
    }
}